<?php
        session_start();
		
		
		if( ! empty($_SESSION['user']) )
		{
				require_once ('./mysql_connect.php'); 

				$rez = mysqli_query($dbc,"SELECT `nume`,`grup` FROM anc.utilizatori WHERE mail='$_SESSION[user]'");	
				$rand = mysqli_fetch_row($rez);
				if( !empty($rand[0]) && !empty($rand[1]) )
				{
					define("USERNAME", $rand[0]);
					define("GROUP", $rand[1]); 
				}
				
				if ( GROUP == "admin" )
				{
					header("Location: admin_main_redirect_pages.php");
					exit;	
				}
				else
				{
					header("Location: user_main_redirect_pages.php");
					exit;	
				}			
		}	
		
		include "includes".DIRECTORY_SEPARATOR."header.php";
		main_header(0);
?>

<tr>
    <td colspan="2" class="text1" height="60" valign="top"></td>
</tr>        
<tr>
    <td style="padding-left:20;padding-right:20;padding-bottom:20" background="images/white.jpg" valign="top" width="400" height="100%">
 <?php
           
            
            $form=<<<GATA
               <br>
			   <h4>Introduceti adresa de email si parola</h4>	
                <br>
               <form method = POST action = "manage_login.php">
               <table border = 0>
               <tr><td><h4>Email</td><td><input type = "text" name = "mail"></td></tr></h4>
               <tr><td><h4>Parola</td><td><input type = "password" name = "pass"></td></tr></h4>
               <tr><td><input type = submit value = "Login" ></td></tr>
               </table>
               </form>
GATA;

            if($_SERVER['REQUEST_METHOD'] == "GET")
			{
				echo $form;
			}
            else if($_SERVER['REQUEST_METHOD'] == "POST")
            {
                    require_once ('./mysql_connect.php'); 
                    $u = mysqli_query($dbc,"SELECT * FROM utilizatori");
					while ($aa = mysqli_fetch_assoc($u))
					{ 
						if($_POST["mail"] == $aa["mail"] && $_POST["pass"] == $aa["parola"])
						{		 
							$_SESSION["user"] = $_POST["mail"];
														
							require_once ('./mysql_connect.php'); 

							$rez = mysqli_query($dbc,"SELECT `nume`,`grup` FROM anc.utilizatori WHERE mail='$_SESSION[user]'");	
							$rand = mysqli_fetch_row($rez);
							
							if( !empty($rand[0]) && !empty($rand[1]) )
							{
								define("USERNAME", $rand[0]);
								define("GROUP", $rand[1]); 
							}
							
							if ( GROUP == "admin" )
							{
								header("Location: admin_main_redirect_pages.php");
							}
							else
							{
								header("Location: user_main_redirect_pages.php");
							}
											 
						}
					}
					
					echo "<br><font color=\"#FF0000\"><b>Autentificare esuata !</b></font><br>";
					echo "<br><font color=\"#FF0000\"><b>Numele de utilizator sau parola au fost introduse gresit !</b></font><br>";	
					
					echo $form;
            }
            
 ?>
    </td>
</tr>


<?php
	include "includes".DIRECTORY_SEPARATOR."footer.php";
?>
